﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;
using Ue.Infrastructure.DomainBase;
using Ue.Qcdz.Domain.Permissions;
using Ue.Qcdz.Domain.Roles;

namespace Ue.Qcdz.Domain.Membership
{
    /// <summary>
    /// User实体
    /// </summary>
    public class User : EntityBase
    {
        private IList<Role> roles;

        public User(object key):base(key)
        {

        }

        public User(string userName, string password)
            : base(null)
        {
            this.roles = new List<Role>();
            this.UserName = userName;
            this.Password = password;
            //this.PasswordQuestion = passwordQuestion;
            this.LastLoginDate = DateTime.Now;
            //this.PasswordAnswer = passwordAnswer;
            //this.PasswordOtherQuestion = otherPasswordQuestion;
            this.Profile = new UserProfile();

        }

        public string UserName { get; private set; }
        public string Password { get; private set; }
        public PasswordQuestion PasswordQuestion { get; private set; }
        public string PasswordOtherQuestion { get; private set; }
        public string PasswordAnswer { get; private set; }
        public DateTime LastLoginDate { get; private set; }
        public EntityState State { get; set; }
        public UserProfile Profile { get; set; }


        public ReadOnlyCollection<Role> Roles
        {
            get { return new ReadOnlyCollection<Role>(this.roles); }
        }

        public bool ChangePassword(string password, string newPassword)
        {
            if (CheckPwd(password))
            {
                this.Password = newPassword;
                return true;
            }
            return false;

        }
      
        public bool CheckPwd(string strPwd)
        {
            return strPwd == this.Password;
        }

        public void ChangeRoleList(List<Role> roleList)
        {
            this.roles = roleList;
        }
        public void ChangeStatus()
        {
            this.State = this.State == EntityState.Disable ? EntityState.Enable : EntityState.Disable;
        }
        /// <summary>
        /// 根据用户信息生成验证票
        /// </summary>
        public void CreateFormsAuthentication()
        {
            FormsAuthentication.SetAuthCookie(this.UserName, false);
        }
        /// <summary>
        /// 退出登录
        /// </summary>
        public void SignOut()
        {
            FormsAuthentication.SignOut();
        }
        public bool IsPermersion(string permersion)
        {
            bool isPermersion = false;

            foreach (Permission permission in Roles.SelectMany(role => role.PermissionList))
            {
                return isPermersion = permission.Name.Equals(permersion);
            }

            return isPermersion;
        }

    }
}
